﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Speedy
{
    public partial class frmPromosyon : XtraForm
    {
        public frmPromosyon()
        {
            InitializeComponent();
        }

        private void frmPromosyon_Load(object sender, EventArgs e)
        {
            List();
        }

        private void List()
        {
            dbPromotionHeader prm = new dbPromotionHeader();
            prm.code.Alias = "Kodu";
            prm.name.Alias = "Tanımı";
            gridControl1.DataSource = prm.AllData();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            dbPromotionHeader prm = new dbPromotionHeader();
            prm.code.SetValue(txtNo.Text);
            prm.name.SetValue(txtAdi.Text);
            prm.amount.SetValue(txtAdet.Text);
            prm.discRate.SetValue(txtYuzde.Text);
            prm.discTotal.SetValue(txtTutar.Text);
            prm.limit.SetValue(txtLimit.Text);
            prm.promoAmount.SetValue(txtPrmAdet.Text);
            prm.type.SetValue(cmbCesit.SelectedIndex + 1);
            prm.Save();
            List();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            dbPromotionHeader prm = new dbPromotionHeader();
            prm.code.SetValue(txtNo.Text);
            prm.Remove();
            List();
        }



        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridView1.RowCount > 0 && gridView1.Columns.Count > 0)
                SelectRecord(e.FocusedRowHandle);
        }

        void SelectRecord(int row)
        {
            dbPromotionHeader prm = new dbPromotionHeader();
            prm.code.SetValue(gridView1.GetRowCellValue(row, gridView1.Columns["Kodu"]).ToString());
            prm.Find();
            txtAdet.Text = prm.amount.Read().ToString();
            txtAdi.Text = prm.name.Read().ToString();
            txtLimit.Text = prm.limit.Read().ToString();
            txtNo.Text = prm.code.Read().ToString();
            txtPrmAdet.Text = prm.promoAmount.Read().ToString();
            txtTutar.Text = prm.discTotal.Read().ToString();
            txtYuzde.Text = prm.discRate.Read().ToString();
            cmbCesit.SelectedIndex = Convert.ToInt32(prm.type.Read()) - 1;
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount > 0 && gridView1.Columns.Count > 0)
                SelectRecord(gridView1.FocusedRowHandle);
        }

    }
}
